package com.donggua.aichebao.commonui.maintain;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by Administrator on 2017-07-23.
 */
//com.donggua.common.ui.maintain.MaintainItemLayout
public class MaintainItemLayout extends LinearLayout{
    private ImageView imgLeft;
    private TextView tvName;
    private TextView tvContent;
    private int margin = 5;
    public MaintainItemLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CustomMaintain);
        margin = (int)context.getResources().getDimension(R.dimen.margin);
        this.setOrientation(LinearLayout.HORIZONTAL);
        this.setGravity(Gravity.CENTER_VERTICAL);
        imgLeft = new ImageView(context);
        LayoutParams imgLayouParams = getMyLayoutParams();
        imgLayouParams.rightMargin = 2*margin;
        Drawable drawable = ta.getDrawable(R.styleable.CustomMaintain_maintainDrawable);
        imgLeft.setImageDrawable(drawable);
        imgLeft.setLayoutParams(imgLayouParams);
        tvName = getMyTextView(context);
        LayoutParams nameLayParams = getMyLayoutParams();
        tvName.setLayoutParams(nameLayParams);
        String name = ta.getString(R.styleable.CustomMaintain_maintainName);
        tvName.setText(name);
        tvContent = getMyTextView(context);
        LayoutParams contentLayParams = getMyLayoutParams();
        contentLayParams.leftMargin = margin;
        tvContent.setLayoutParams(contentLayParams);
        String content = ta.getString(R.styleable.CustomMaintain_maintainContent);
        tvContent.setText(content);
        this.addView(imgLeft);
        this.addView(tvName);
        this.addView(tvContent);
        ta.recycle();
    }

    private TextView getMyTextView(Context context){
        TextView tv = new TextView(context);
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(0x77777777);
        //tv.setTextSiz
        return  tv;
    }

    private static LayoutParams getMyLayoutParams(){
        LayoutParams layoutParams = new LayoutParams(
                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        return layoutParams;
    }

    public MaintainItemLayout(Context context) {
        super(context);
    }
}
